package com.atguigu.dga.governance.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.service.GovernanceAssessDetailService;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/governance")
public class GovernanceController {

    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;

    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;

    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    @GetMapping("/globalScore")
    public String getGlobalScore(){
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(new QueryWrapper<GovernanceAssessGlobal>().inSql("assess_date", "select max(assess_date) from governance_assess_global"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assessDate",governanceAssessGlobal.getAssessDate());
        jsonObject.put("sumScore",governanceAssessGlobal.getScore());
        List scoreList=new ArrayList();
        scoreList.add(governanceAssessGlobal.getScoreSpec());
        scoreList.add(governanceAssessGlobal.getScoreStorage());
        scoreList.add(governanceAssessGlobal.getScoreCalc());
        scoreList.add(governanceAssessGlobal.getScoreQuality());
        scoreList.add(governanceAssessGlobal.getScoreSecurity());
        jsonObject.put("scoreList",scoreList);
        return jsonObject.toJSONString();
    }


    @GetMapping("/rankList")
    public String getRankList(){
        List<Map<String, Object>> mapList = governanceAssessTecOwnerService.listMaps(
                new QueryWrapper<GovernanceAssessTecOwner>()
                        .select("tec_owner as tecOwner", "score")
                        .inSql("assess_date", "select max(assess_date) from governance_assess_tec_owner")
                        .orderByDesc("score"));

        String jsonString = JSON.toJSONString(mapList);
        return jsonString;
    }



    @GetMapping("/problemNum")
    public String getProblemNum(){
        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .select("count(if(assess_score<10 and governance_type ='SPEC',1,null )) as  SPEC",
                        " count(if(assess_score<10 and governance_type ='STORAGE',1,null )) as  STORAGE",
                        "count(if(assess_score<10 and governance_type ='CALC',1,null )) as  CALC",
                        "count(if(assess_score<10 and governance_type ='QUALITY',1,null )) as  QUALITY",
                        " count(if(assess_score<10 and governance_type ='SECURITY',1,null )) as  SECURITY")
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail");


        Map<String, Object> map = governanceAssessDetailService.getMap(queryWrapper);

        return JSON.toJSONString(map);
    }

    @GetMapping("/problemList/{governType}/{pageNo}/{pageSize}")
    public String getProblemList(@PathVariable("governType") String governType,
                                 @PathVariable("pageNo") int pageNo,
                                 @PathVariable("pageSize") int pageSize){
        int rowNo=(pageNo-1)*pageSize;
        List<GovernanceAssessDetail> governanceAssessDetailList = governanceAssessDetailService.list(new QueryWrapper<GovernanceAssessDetail>()
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                .eq("governance_type", governType)
                        .lt("assess_score",10)
                .last("limit " + rowNo + "," + pageSize));

        return JSON.toJSONString(governanceAssessDetailList);

    }
}
